package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.Refresh;
import com.example.prison.service.RefreshService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("刷新设置")
@RestController
@RequestMapping("/api")
public class RefreshController {

    @Autowired
    private RefreshService refreshService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取刷新信息")
    @GetMapping("/getRefresh")
    public Response<PageInfo<Refresh>> getRefresh(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取刷新信息");
        PageInfo<Refresh> pageInfo = refreshService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据id获取刷新信息")
    @GetMapping("/getRefreshById")
    public Response<Refresh> getRefreshById(@RequestParam(value = "id") Integer id) {
        log.info("依据id获取刷新信息");
        systemLogService.add("依据id获取刷新信息");
        Refresh re = refreshService.getById(id);
        if(ObjectUtils.isEmpty(refreshService.getById(id))) {
            return Response.error("id不存在！");
        }
        return Response.success("获取成功！", re);
    }

    @ApiOperation("新增刷新设置")
    @PostMapping("/addRefresh")
    public Response<?> addMap(@RequestBody Refresh rate) {
        log.info("新增刷新设置");
        systemLogService.add("新增刷新设置");
        int result = refreshService.add(rate);
        return Response.success("添加成功！");
    }

    @ApiOperation("编辑刷新设置")
    @PostMapping("/updateRefresh")
    public Response<?> updateRefresh(@Parameter(description = "ID") @RequestParam(value = "id") int id,
                                     @Parameter(description = "刷新频率") @RequestParam(value = "rate") int rate) {
        log.info("编辑刷新设置");
        systemLogService.add("编辑刷新设置");
        if(ObjectUtils.isEmpty(refreshService.getById(id))) {
            return Response.error("id不存在！");
        }
        refreshService.update(id, rate);
        return Response.success("编辑成功！");

    }
}
